#!/usr/bin/env bash
set -e

apt install universal-ctags -y

# 设置环境变量
export PATH="/usr/bin:/usr/local/bin:$HOME/go/bin:$PATH"
export LC_ALL="zh_CN.UTF-8"
export LANG="zh_CN.UTF-8"
export LANGUAGE="zh_CN.UTF-8"
export EDITOR="vim"

# clash 和 proxy 配置
export clashdir="/usr/share/clash"
alias clash="bash /usr/share/clash/clash.sh"
alias unsetproxy="unset ALL_PROXY"
alias setproxy="export ALL_PROXY=socks5://127.0.0.1:7890"

# 变量定义
VIM_SRC_DIR="/opt/src"
VIMBUILD_DIR="${VIM_SRC_DIR}/vim/src"

# 更新系统
apt update -y && apt upgrade -y

# 安装 nvm
if ! [ $(command -v "git") ]; then
   if ! [ $(command -v "git") ]; then
      echo "=> 请您首先安装Git"
      exit 127
   fi
   export NVM_DIR="$HOME/.nvm" && (
      echo "=> Git clone nvm"
      git clone https://gitee.com/RubyKids/nvm-official "$NVM_DIR" # 备用仓库
      # git clone https://gitee.com/mirrors_nvm-sh/nvm "$NVM_DIR"  # Gitee提供的追踪上游仓库
      cd "$NVM_DIR"
      git checkout $(git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1))
   ) && \. "$NVM_DIR/nvm.sh"

   echo "=> 添加nvm环境变量(Zsh)"
   echo -e "\n# nvm config" >>~/.zshrc

   echo "export NVM_DIR=\"\$HOME/.nvm\"" >>~/.zshrc
   echo "[ -s \"\$NVM_DIR/nvm.sh\" ] && \\. \"\$NVM_DIR/nvm.sh\" # This loads nvm" >>~/.zshrc

   echo "=> 使用淘宝镜像"
   echo "export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node" >>~/.zshrc

   echo "=> 安装nvm-update,升级更新请使用该命令"
   curl -fsSL https://gitee.com/RubyKids/nvm-cn/raw/main/nvm-update.sh -o nvm-update.sh
   chmod +x ./nvm-update.sh

   if ! [ -v NVM_CN_IN_CI ]; then
      sudo mv ./nvm-update.sh /usr/local/bin/nvm-update
   fi
   source ~/.zshrc
   echo "=> 安装完成!"
fi

# 安装 Node 和 npm
if ! [ $(command -v "node") ]; then
   echo "=> 安装 Node 和 npm"
   nvm --version
   nvm install --lts
   nvm install-latest-npm
   # 配置 npm 源为淘宝镜像，并安装 yarn、pnpm、pm2、npm-check 等工具包
   npm config set registry https://registry.npmmirror.com
   npm install -g yarn
   yarn config set registry https://registry.npmmirror.com
   npm install -g pnpm
   pnpm config set registry https://registry.npmmirror.com
   npm install -g npm-check

   # 全局更新工具包版本
   npm-check -u -g
fi

#-------------------------------------------------
## 下载 vim 源码并编译安装 vim

# 创建源码目录并下载 vim 源码到指定目录中
if [ -d "/opt/src" ]; then
   mkdir -p /opt/src
fi
cd ${VIM_SRC_DIR} || exit

if [ -d "/opt/src/vim" ]; then
   echo "vim 源代码已存在，删除 vim 源代码..."
   rm -rf ${VIM_SRC_DIR}/vim
fi

git clone --depth 1 https://gitee.com/synchub/vim.git

## 卸载 vim（如果已安装）
if command -v vim &>/dev/null; then
   # killall vim # 关闭所有正在运行的 vim 进程
   apt remove --purge vim vim-runtime vim-gnome vim-tiny vim-common -y
   cd ${VIMBUILD_DIR} || exit
   make uninstall
   rm -rf /usr/local/bin/vim*
   rm -rf /usr/local/share/vim
   rm -rf /usr/bin/vim
   rm -rf /usr/share/vim
   rm -rf /usr/local/lib/libvim*
   echo "Vim已完全卸载"
else
   echo "vim 未安装"
fi

# 配置 vim 的安装选项。它指定了安装目录、启用的功能和解释器、图形界面支持等选项
CACHE_FILE="/opt/src/vim/src/auto/config.cache"

if [ -f $CACHE_FILE ]; then
   make disclean
   rm auto/config.cache
fi

./configure --prefix=/usr/local \
   --with-features=huge \
   --enable-multibyte \
   --enable-luainterp=yes \
   --enable-mzschemeinterp \
   --enable-perlinterp=yes \
   --enable-python3interp=yes \
   --enable-tclinterp=yes \
   --enable-rubyinterp=yes \
   --enable-terminal \
   --enable-autoservername \
   --enable-multibyte \
   --enable-xim \
   --enable-fontset \
   --enable-gui=gtk3 \
   --enable-gtk3-check=yes \
   --with-vim-name=vim \
   --with-modified-by=Maximo \
   --enable-gpm \
   --enable-cscope

# 编译 vim，指定运行时目录和并行编译线程数
make VIMRUNTIMEDIR=/usr/local/share/vim/vim90 -j4
# 安装 vim
make install

# 删除原有的 vim 配置文件
rm -rf $HOME/.vimrc

# 下载 vim-plug 插件管理器
curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://gitee.com/synchub/vim-plug/raw/master/plug.vim
# 下载 vim 配置文件
curl -o /usr/local/share/vim/vim90/colors/gruvbox.vim https://gitee.com/jonnyhub/raspi-config/raw/master/source/gruvbox.vim
curl -o $HOME/.vimrc https://gitee.com/jonnyhub/raspi-config/raw/master/source/vimrc
curl -o $HOME/.vimrc.custom.plugins https://gitee.com/jonnyhub/raspi-config/raw/master/source/vimrc_custom_plugins
curl -o $HOME/.vimrc.custom.config https://gitee.com/jonnyhub/raspi-config/raw/master/source/vimrc_custom_config

# 启动 vim
vim

# 退出脚本
exit 0
